package com.example.project.mapper;

import java.util.List;

import com.example.project.entity.TimeEntrie;
import org.apache.ibatis.annotations.*;

/**
 * 时间记录Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-17
 */
@Mapper
public interface TimeEntrieMapper {

	/**
	 * 查询时间记录
	 * 
	 * @param id 时间记录主键
	 * @return 时间记录
	 */
	@Select("SELECT id, project_id, user_id, author_id, issue_id, hours, comment, activity_id, spent_on, tyear, tmonth, tweek, del_flag, create_by, create_time, update_by, update_time FROM t_time_entrie WHERE id = #{id}")
	public TimeEntrie selectTimeEntriesById(Long id);

	/**
	 * 查询时间记录列表
	 * 
	 * @param timeEntries 时间记录
	 * @return 时间记录集合
	 */
	@Select("<script>"
			+ "SELECT id, project_id, user_id, author_id, issue_id, hours, comment, activity_id, spent_on, tyear, tmonth, tweek, "
			+ "del_flag, create_by, create_time, update_by, update_time FROM t_time_entrie where del_flag = 0 "
			+ "<if test='t.projectId != null'> and project_id = #{t.projectId}</if>"
			+ "<if test='t.userId != null'> and user_id = #{t.userId}</if>"
			+ "<if test='t.authorId != null'> and author_id = #{t.authorId}</if>"
			+ "<if test='t.issueId != null'> and issue_id = #{t.issueId}</if>"
			+ "<if test='t.hours != null'> and hours = #{t.hours}</if>"
			+ "<if test='t.comment != null and t.comment != \"\"'> and comment = #{t.comment}</if>"
			+ "<if test='t.activityId != null'> and activity_id = #{t.activityId}</if>"
			+ "<if test='t.spentOn != null'> and spent_on = #{t.spentOn}</if>"
			+ "<if test='t.tyear != 0'> and tyear = #{t.tyear}</if>"
			+ "<if test='t.tmonth != 0'> and tmonth = #{t.tmonth}</if>"
			+ "<if test='t.tweek != 0'> and tweek = #{t.tweek}</if>" + "${t.params.dataScope} </script>")
	public List<TimeEntrie> selectTimeEntriesList(@Param("t") TimeEntrie timeEntries);

	/**
	 * 新增时间记录
	 * 
	 * @param timeEntries 时间记录
	 * @return 结果
	 */
	@Insert("<script>" + "INSERT INTO t_time_entrie" + "<trim prefix='(' suffix=')' suffixOverrides=','>"
			+ "<if test='projectId != null'>project_id,</if>" + "<if test='userId != null'>user_id,</if>"
			+ "<if test='authorId != null'>author_id,</if>" + "<if test='issueId != null'>issue_id,</if>"
			+ "<if test='hours != null'>hours,</if>" + "<if test='comment != null'>comment,</if>"
			+ "<if test='activityId != null'>activity_id,</if>" + "<if test='spentOn != null'>spent_on,</if>"
			+ "<if test='tyear != null'>tyear,</if>" + "<if test='tmonth != null'>tmonth,</if>"
			+ "<if test='tweek != null'>tweek,</if>" + "<if test='delFlag != null'>del_flag,</if>"
			+ "<if test='createBy != null'>create_by,</if>" + "<if test='createTime != null'>create_time,</if>"
			+ "<if test='updateBy != null'>update_by,</if>" + "<if test='updateTime != null'>update_time,</if>"
			+ "</trim>" + "<trim prefix='values (' suffix=')' suffixOverrides=','>"
			+ "<if test='projectId != null'>#{projectId},</if>" + "<if test='userId != null'>#{userId},</if>"
			+ "<if test='authorId != null'>#{authorId},</if>" + "<if test='issueId != null'>#{issueId},</if>"
			+ "<if test='hours != null'>#{hours},</if>" + "<if test='comment != null'>#{comment},</if>"
			+ "<if test='activityId != null'>#{activityId},</if>" + "<if test='spentOn != null'>#{spentOn},</if>"
			+ "<if test='tyear != null'>#{tyear},</if>" + "<if test='tmonth != null'>#{tmonth},</if>"
			+ "<if test='tweek != null'>#{tweek},</if>" + "<if test='delFlag != null'>#{delFlag},</if>"
			+ "<if test='createBy != null'>#{createBy},</if>" + "<if test='createTime != null'>#{createTime},</if>"
			+ "<if test='updateBy != null'>#{updateBy},</if>" + "<if test='updateTime != null'>#{updateTime},</if>"
			+ "</trim>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertTimeEntries(TimeEntrie timeEntries);

	/**
	 * 修改时间记录
	 * 
	 * @param timeEntries 时间记录
	 * @return 结果
	 */
	@Update("<script>" + "UPDATE t_time_entrie" + "<trim prefix='SET' suffixOverrides=','>"
			+ "<if test='projectId != null'>project_id = #{projectId},</if>"
			+ "<if test='userId != null'>user_id = #{userId},</if>"
			+ "<if test='authorId != null'>author_id = #{authorId},</if>"
			+ "<if test='issueId != null'>issue_id = #{issueId},</if>"
			+ "<if test='hours != null'>hours = #{hours},</if>"
			+ "<if test='comment != null'>comment = #{comment},</if>"
			+ "<if test='activityId != null'>activity_id = #{activityId},</if>"
			+ "<if test='spentOn != null'>spent_on = #{spentOn},</if>"
			+ "<if test='tyear != null'>tyear = #{tyear},</if>" + "<if test='tmonth != null'>tmonth = #{tmonth},</if>"
			+ "<if test='tweek != null'>tweek = #{tweek},</if>"
			+ "<if test='delFlag != null'>del_flag = #{delFlag},</if>"
			+ "<if test='createBy != null'>create_by = #{createBy},</if>"
			+ "<if test='createTime != null'>create_time = #{createTime},</if>"
			+ "<if test='updateBy != null'>update_by = #{updateBy},</if>"
			+ "<if test='updateTime != null'>update_time = #{updateTime},</if>" + "</trim>" + "WHERE id = #{id}"
			+ "</script>")
	public int updateTimeEntries(TimeEntrie timeEntries);

	/**
	 * 删除时间记录
	 * 
	 * @param id 时间记录主键
	 * @return 结果
	 */
	@Delete("DELETE FROM t_time_entrie WHERE id = #{id}")
	public int deleteTimeEntriesById(Long id);

	/**
	 * 批量删除时间记录
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Delete("<script>" + "DELETE FROM t_time_entrie WHERE id IN "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	public int deleteTimeEntriesByIds(String[] ids);
}
